import json
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import status
from user.models import *
from .serializer import *
from clbum.serializer import *
from utils.jwt_token import check_login


# 学校
class PrincipalView(ViewSet):

    # 添加学校
    @check_login
    def add_principal(self, request):
        name = request.data.get("name")
        about = request.data.get("about")

        try:
            school = SchoolModel.objects.filter(name=name, is_delete=False)
            if school:
                return Response({"code": status.HTTP_204_NO_CONTENT, "msg": "此学校已经存在"})

            data = {
                "name": name,
                "about": about
            }
            ser = SchoolSer(data=data)
            if not ser.is_valid():
                return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': ser.errors})
            ser.save()

            return Response({"code": status.HTTP_200_OK, "msg": "添加成功"})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 获取所有学校
    @check_login
    def list_principal(self, request):
        try:
            schoole = SchoolModel.objects.filter(is_delete=False).all()
            ser = SchoolSer(schoole, many=True).data
            return Response({"code": status.HTTP_200_OK, "msg": "成功", "data": ser})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 获取单个学校
    @check_login
    def single_principal(self, request, pk):
        try:
            school = SchoolModel.objects.filter(id=pk, is_delete=False).first()
            ser = SchoolSer(school).data
            return Response({"code": status.HTTP_200_OK, "msg": "成功", "data": ser})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 修改单个学校
    @check_login
    def up_principal(self, request, pk):
        name = request.data.get("name")
        about = request.data.get("about")

        try:
            school = SchoolModel.objects.filter(id=pk, is_delete=False)

            if not school:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg": "没有此学校"})

            if name:
                school.update(name=name)
            if about:
                school.update(about=about)
            return Response({"code": status.HTTP_200_OK, "msg": "成功"})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 删除单个学校
    @check_login
    def del_principal(self, request, pk):
        try:
            school = SchoolModel.objects.filter(id=pk, is_delete=False)
            if not school:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg": "没有此学校"})
            school.update(is_delete=True)
            return Response({"code": status.HTTP_200_OK, "msg": "成功"})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 获取一个学校的所有班级
    @check_login
    def list_school(self, request, pk):
        try:
            school = SchoolModel.objects.filter(id=pk, is_delete=False).first()
            if not school:
                return Response({"code": status.HTTP_204_NO_CONTENT, "msg": "没有此学校"})

            ser = SchoolSer(school).data
            ser = json.loads(json.dumps(ser))
            print("ser", ser)

            classs = ClasssModel.objects.filter(school_id=ser['id'], is_delete=False).all()
            classs_ser = ClasssSer(classs, many=True).data
            classs = json.loads(json.dumps(classs_ser))
            ser['classs_list'] = classs

            return Response({"code": status.HTTP_200_OK, "msg": "成功", "data": ser})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})



